using System;
using FireStarter.Base.BusinessObjectMerger;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;

namespace FireStarterModeller.Logic.MergeBusinessObjects
{
    // This classes are used automatically by the MergeableBORegistry
    // ReSharper disable UnusedMember.Global

    public class MergeDMProperty : MergeBO<DMProperty>
    {
        protected override void MergeSingleRelationship(ISingleRelationship fromSingleRelationship, ISingleRelationship toSingleRelationship)
        {
            //This is needed to ensure that the correct Lookup List type is created in the merge.
            //LookupList is sub typed into simple, Business etc.
            if (toSingleRelationship != null && fromSingleRelationship != null && fromSingleRelationship.RelationshipName == "LookupList")
            {
                if(toSingleRelationship.GetRelatedObject() == null && fromSingleRelationship.GetRelatedObject() != null)
                {
                    toSingleRelationship.SetRelatedObject(fromSingleRelationship.GetRelatedObject().ClassDef.CreateNewBusinessObject());
                }
            }
            base.MergeSingleRelationship(fromSingleRelationship, toSingleRelationship);
        }

        protected override void MergeProperties(IBusinessObject fromMergableBo, IBusinessObject toMergableBo)
        {
            //This custom merge is needed so as to merge the toDMProp with the correct data type.
            base.MergeProperties(fromMergableBo, toMergableBo);
            DMProperty fromDMProp = (DMProperty)fromMergableBo;
            DMProperty toDMProp = (DMProperty)toMergableBo;
            if (fromDMProp.PropertyType != null && toDMProp.Solution != null)
            {
                //This ensures that even if a custom type has been used in the ClassDef.xml i.e. a type that
                // will not normally be created as a standard type. The property type will be created on merging the 
                // ClassDefs.
                var dmPropertyType = DMPropertyType.FindOrCreate((DMSolution)toDMProp.Solution, fromDMProp.PropertyType.AssemblyName, fromDMProp.PropertyType.PropertyTypeName);
                toDMProp.PropertyType = dmPropertyType;
            }
        }

        public override bool MustMergeRelationship(IRelationship relationship)
        {
            //This is needed because LookupList relationship is not marked as Aggregation or Composition.
            if(relationship.RelationshipName == "LookupList")
            {
                return true;
            }
            return base.MustMergeRelationship(relationship);
        }
    }

    public class MergeDMLookupListSimple : MergeBO<DMLookupList>
    {
        protected override void MergeMultipleRelationship(IMultipleRelationship fromMultipleRelationship, IMultipleRelationship toMultipleRelationship)
        {
            //Remove all the ListItems for the Relationship so that they can be created from scratch from the fromRelationship.
            //Since there is no values on the ListItem to match and find from.
            if (fromMultipleRelationship.RelationshipName == "ListItems")
            {
                while (toMultipleRelationship.CurrentBusinessObjectCollection.Count > 0)
                {
                    var relProp = toMultipleRelationship.CurrentBusinessObjectCollection[0];
                    relProp.MarkForDelete();
                }
            }
            base.MergeMultipleRelationship(fromMultipleRelationship, toMultipleRelationship);
        }
    }
// ReSharper restore UnusedMember.Global
}